package cn.yr.common.oss.factory;


import cn.yr.common.oss.Oss;
import cn.yr.common.oss.UpyunOss;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @ClassName : UpyunOssFactory
 * @Description : 又拍云工厂
 * @Author : Lang
 * @Date: 2020-03-07 08:50
 */
public class UpyunOssFactory implements IOssFactory{

    /**
     * 服务器名
     */
    private String bucket;

    /**
     * 操作员名
     */
    private String username;

    /**
     * 操作员密码
     */
    private String password;

    /**
     * 外链域名
     */
    private String domain;


    private static UpyunOss upyunOss;

    void init(){
        Properties resouce = getResouce();
        this.username = resouce.getProperty("upy.username");
        this.password = resouce.getProperty("upy.password");
        this.bucket = resouce.getProperty("upy.bucket");
        this.domain = resouce.getProperty("upy.domain");
    }

    private Properties getResouce(){
        // 获取输入流
        InputStream in = getClass().getClassLoader().getResourceAsStream("oss.properties");
        // 新建配置对象
        Properties p = new Properties();
        // 通过输入流加载
        try {
            p.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 读取key
        return  p;
    }

    @Override
    public Oss getOss() {
        if(upyunOss!=null){
            return upyunOss;
        }
        init();
        UpyunOss upyunOss = new UpyunOss(this.username,this.password,this.bucket,this.domain);
        UpyunOssFactory.upyunOss = upyunOss;
        return upyunOss;
    }


    public Oss getOss(String username,String password,String bucket,String domain) {
        UpyunOss upyunOss = new UpyunOss(username,password,bucket,domain);
        UpyunOssFactory.upyunOss = upyunOss;
        return upyunOss;
    }
}
